<template>
  <div class="opera-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="DocumentAdd" plain type="primary" @click="openAdd">
          新增
        </el-button>
        <el-button :icon="Edit" plain type="warning" @click="update">
          修改
        </el-button>
        <el-button :icon="Delete" plain type="danger" @click="deleteWc">
          删除
        </el-button>
        <!-- <el-button :icon="Edit" plain type="warning" @click="toUpdate()">
          停用
        </el-button>
        <el-button :icon="Edit" plain type="primary" @click="toUpdate()">
          启用
        </el-button> -->
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '4px 0' }"
      :data="state.opData"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="工序编码" prop="opCode" width="180" />
      <el-table-column label="工序名称" prop="opName" width="180" />
      <el-table-column label="工作中心" prop="wcName" width="180" />
      <!-- <el-table-column label="首检" width="120">
        <template #default="scope">
          <el-icon v-if="scope.row.isShoujian" color="#0a3784" size="22">
            <Check />
          </el-icon>
          <div v-else></div>
        </template>
      </el-table-column> -->
      <el-table-column label="绩效" width="120">
        <template #default="scope">
          <el-icon v-if="scope.row.isKpi" color="#0a3784" size="22">
            <Check />
          </el-icon>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createUser" width="120px" />
      <el-table-column label="创建时间" prop="createTime" width="200" />
      <el-table-column label="备料仓库" prop="blWhName" width="180" />
      <el-table-column label="入库仓库" prop="defaultRkWhName" width="180" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <addOp ref="addOpRef" @success-callback="getOp" />
  </div>
</template>

<script setup>
  import { DocumentAdd, Check, Edit, Delete } from '@element-plus/icons-vue'
  import { getOperPageApi } from '@/api/base/op'
  import { localStorageConfig } from '@/config/localstorage.config'
  import addOp from './modules/addOp.vue'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const state = reactive({
    opData: [],
    currentRow: {},
  })

  const wcQueryRef = ref({ pkCorp: pkCorp, dr: 0 })

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const addOpRef = ref()

  const getOp = async () => {
    const load = $baseLoading()
    try {
      let params = {
        pageNum: paginationParams.pageNum,
        pageSize: paginationParams.pageSize,
        ...wcQueryRef.value,
      }
      const res = await getOperPageApi(params)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        state.opData = res.data.list
        paginationParams.total = res.data.total
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openAdd = () => addOpRef.value.openDialog()

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  watch(paginationParams, () => getOp())

  onMounted(() => getOp())
</script>
